#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
using namespace std;
#define BUFSIZE 512
static void bail(const char *on_what)
{
    fputs(strerror(errno), stderr);
    fputs(": ", stderr);
    fputs(on_what, stderr);
    fputc('\n', stderr);
    exit(1);
}
int main(int argc, char *argv[])
{
    int sockfd;
    int new_fd;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    socklen_t size;
    int portnumber;
    char reqBuf[BUFSIZE];
    int z;
    if (argc != 2)
    {
        fprintf(stderr, "错误\n");
        exit(1);
    }
    if ((portnumber = atoi(argv[1])) < 0)
    {
        fprintf(stderr, "错误\n");
        exit(1);
    }
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    {
        fprintf(stderr, "Socket error:%s\a\n", strerror(errno));
        exit(1);
    }
    memset(&server_addr, 0, sizeof server_addr);
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(portnumber);
    if ((bind(sockfd, (struct sockaddr *)(&server_addr), sizeof server_addr)) == -1)
    {
        fprintf(stderr, " Bind error:%s\a\n", strerror(errno));
        exit(1);
    }
    if (listen(sockfd, 128) == -1)
    {
        fprintf(stderr, "Listen error:%s\a\n", strerror(errno));
        exit(1);
    }
    cout << " waiting for the client's request..." << endl;
    while (1)
    {
        size = sizeof(struct sockaddr_in);
        new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &size);
        if (new_fd == -1)
        {
            fprintf(stderr, "Accept error:%s\a\n", strerror(errno));
            exit(1);
        }
        fprintf(stdout, "Server got connection fom %sn", inet_ntoa(client_addr.sin_addr));
        for (;;)
        {
            z = read(new_fd, reqBuf, sizeof(reqBuf));
            cout << "the length of the String is: " << z << endl;
            if (z < 0)
                bail("read()");
            if (z == 0)
            {
                close(new_fd);
                cout<<"the client leaves"<<endl;
            }
            reqBuf[z] = 0;
            int num = 0;
            for (int j = 0; j < z; j++)
            {
                if (reqBuf[j] <= 'z' && reqBuf[j] >= 'a' || reqBuf[j] <= 'Z' && reqBuf[j] >= 'A')
                    num++;
            }
            z = write(new_fd, &num, sizeof(int));
            cout << "the number of the English letters is:" << num << endl;
            if (z < 0)
                bail("'write()");
        }
    }
}